{{template "admin/header.html"}}
<style>
	.layui-form-label {
	  width: 120px;
	}
	.layui-input-block {
	  margin-left: 150px;
	}
</style>
<title>{{T "Update Navigation Menu"}} - GPRESS</title>
{{template "admin/bodystart.html"}}

{{ $category := selectOne "category" "* from category WHERE id=?" .Data.Pid }}
{{ $themeTemplate := themeTemplate "category" }} <!-- 模版数据 -->
<div class="layui-card layui-panel" style="height: 100%;">
	<div class="layui-card-header">
	  {{T "Update Navigation Menu"}}
	</div>
	<div class="layui-card-body">
	  <form class="layui-form" id="gpress-form" action="{{basePath}}admin/{{.UrlPathParam}}/update" method="POST">
		  <div class="layui-form-item layui-col-md6">
			<label class="layui-form-label">{{T "Path Identifier"}}</label>
			<div class="layui-input-block">
			  <input type="hidden" name="id" id="id" value="{{.Data.Id}}" />
			  <input type="text" class="layui-input" disabled value="{{lastURI .Data.Id}}" />
			</div>
		  </div>
		  <div class="layui-form-item layui-col-md6">
			<label class="layui-form-label">{{T "URL Path"}}</label>
			<div class="layui-input-block">
			  <input type="text" class="layui-input" disabled value="{{basePath}}{{trimSlash .Data.Id}}" />
			</div>
		  </div>
		  <div class="layui-form-item layui-col-md6">
			<label class="layui-form-label">{{T "Navigation Name"}}</label>
			<div class="layui-input-block">
			  <input type="text" name="name" lay-verify="required" lay-reqtext='{{T "Please fill in the navigation name"}}' autocomplete="off" class="layui-input" value="{{.Data.Name}}" />
			</div>
		</div>

		<div class="layui-form-item layui-col-md6">
			<label class="layui-form-label">{{T "Redirect Path"}}</label>
			<div class="layui-input-block">
			  <input type="text" name="hrefURL" autocomplete="off" class="layui-input"  value="{{.Data.HrefURL}}" />
			</div>
		</div>

		<div class="layui-form-item layui-col-md6">
		  <label class="layui-form-label">{{T "Redirect Method"}}</label>
		  <div class="layui-input-block">
			  <select name="hrefTarget" id="hrefTarget">
				  <option value="">{{T "Default"}}</option>
				  <option value='_blank'>_blank</option>
				  <option value='_parent'>_parent</option>
				  <option value='_self'>_self</option>
				  <option value='_top'>_top</option>
			  </select>
		  </div>
		</div>

		<div class="layui-form-item layui-col-md6">
		  <label class="layui-form-label">{{T "Parent Navigation"}}</label>
		  <div class="layui-input-block">
			<input  type="hidden"  name="pid"  value="{{$category.Id}}">
			<input type="text"  autocomplete="off" class="layui-input" value="{{$category.Name}}" disabled>
		  </div>
		</div>

		<div class="layui-form-item layui-col-md6">
		  <label class="layui-form-label">{{T "Page Template"}}</label>
		  <div class="layui-input-block">
			  <select name="templateFile" id="templateFile">
				  <option value="">{{T "Default"}} (category.html)</option>
				  {{ range $index,$obj := $themeTemplate }}
				  <option value='{{$obj.Name}}'>{{$obj.Name}}</option>
				  {{end}}
			  </select>
		  </div>
		</div>

		<div class="layui-form-item layui-col-md6">
			<label class="layui-form-label">Keyword</label>
			<div class="layui-input-block">
			  <input type="text" name="keyword" autocomplete="off" class="layui-input"  value="{{.Data.Keyword}}" />
			</div>
		</div>

		<div class="layui-form-item layui-col-md6">
		  <label class="layui-form-label">Description</label>
		  <div class="layui-input-block">
			<input type="text" name="description" autocomplete="off" class="layui-input"  value="{{.Data.Description}}" />
		  </div>
		</div>

		<div class="layui-form-item layui-col-md6">
			<label class="layui-form-label">{{T "Sort"}}</label>
			<div class="layui-input-block">
			  <input type="number" name="sortNo" lay-verify="required" lay-reqtext='{{T "Please fill in the sort number"}}' autocomplete="off" class="layui-input" value="{{.Data.SortNo}}" />
			</div>
		</div>

		<div class="layui-form-item layui-col-md6">
		  <label class="layui-form-label">{{T "Status"}}</label>
		  <div class="layui-input-block">
			  <select name="status" id="status">
				  <option value="1">{{T "Public"}}</option>
				  <option value="0">{{T "Link Access"}}</option>
				  <option value="2">{{T "Top"}}</option>
				  <option value="3">{{T "Private"}}</option>
			  </select>
		  </div>
		</div>		  

		  <div class="layui-form-item">
			  <div class="layui-input-block">
				<button type="submit" class="layui-btn layui-bg-blue" lay-submit lay-filter="gpress-form-ajax-update">{{T "Submit Changes"}}</button>
			  </div>
		  </div>
	  </form>
	</div>
  </div>
{{template "admin/bodyend.html"}}

<script>
layui.use(function(){
var form = layui.form;
var layer = layui.layer;
var $ =layui.jquery;

//选中跳转方式
$("#hrefTarget option[value='{{.Data.HrefTarget}}']").attr("selected", true);
//选中模板
$("#templateFile option[value='{{.Data.TemplateFile}}']").attr("selected", true);
//选中状态
$("#status option[value='{{.Data.Status}}']").attr("selected", true);

// 渲染全部表单
form.render(); 

// 提交事件
form.on('submit(gpress-form-ajax-update)', function(data){
  var field = data.field; // 获取表单字段值
  field.sortNo=field.sortNo-0;
  field.status=field.status-0;
  field.timeout=field.timeout-0;
  const form = document.getElementById('gpress-form');
  $.ajax({
	  url:form.action,
	  type:form.method,
	  contentType: "application/json;charset=utf-8",
	  dataType:"json",
	  data:JSON.stringify(field),
	  error: function (result) {
		layer.msg('{{T "Update error!"}}'+result.responseJSON.message);
	  },
	  success: function (result) {
					if (result.statusCode == 1) {
						layer.confirm('{{T "Update successful, continue editing?"}}', {
							icon: 3,
							title: '{{T "Confirm"}}',
							btn: ['{{T "Return to List"}}', '{{T "Continue Editing"}}'] //按钮
						}, function () {
							window.location.href = '{{basePath}}admin/content/list';
						});
					} else {
						layer.msg('{{T "Update failed!"}}');
					}
				}
  });
  return false; // 阻止默认 form 跳转
});
});
</script>